package com.qyhl.module_practice.rank.team;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeTeamBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_RANK_TEAM)
public class PracticeTeamRankActivity extends BaseActivity implements PracticeTeamRankContract.PracticeTeamRankView {
    @BindView(R2.id.back_btn)
    TextView backBtn;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private CommonAdapter<PracticeTeamBean> commonAdapter;

    private List<PracticeTeamBean> mDataList = new ArrayList<>();

    private int page = 1;

    private int type; //1 时长  2  次数  3 积分

    private PracticeTeamRankPresenter mPresenter;

    private String instId;

    private int volId;

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_rank_team;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.fullScreen(true).statusBarDarkFont(false).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new PracticeTeamRankPresenter(this);
        type = getIntent().getIntExtra("type", 1);
        instId = getIntent().getStringExtra("instId");
        volId = getIntent().getIntExtra("id", 0);

        switch (type) {
            case 1:
                backBtn.setText("团体服务时长排行");
                break;
            case 2:
                backBtn.setText("团体服务项目排行");
                break;
            case 3:
                backBtn.setText("团体服务积分排行");
                break;
                default:
                    break;
        }

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.practice_item_news_divider));
        recycleView.addItemDecoration(divider);

        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeTeamBean>(this, R.layout.practice_item_rank_team, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PracticeTeamBean bean, int position) {
                holder.setText(R.id.title, bean.getName());

                switch (type) {
                    case 1:
                        holder.setText(R.id.value_num, DateUtils.PracticeServiceTimeFormat(bean.getActTimes()));
                        break;
                    case 2:
                        holder.setText(R.id.value_num, bean.getActNums() + "");
                        break;
                    case 3:
                        holder.setText(R.id.value_num, bean.getActScore() + "");
                        break;
                        default:break;
                }

                switch (position) {
                    case 0:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no1_icon);

                        break;
                    case 1:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no2_icon);

                        break;
                    case 2:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no3_icon);

                        break;
                    default:
                        holder.setVisible(R.id.rank_num, true);
                        holder.setVisible(R.id.rank_num_tag, false);

                        holder.setText(R.id.rank_num, "No." + (position + 1));
                        break;
                }
            }
        });

        mPresenter.getList(page + "", type);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                mPresenter.getList(page + "", type);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getList(page + "", type);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(page + "", type);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                Bundle bundle = new Bundle();
                bundle.putString("orgId", mDataList.get(position).getId() + "");
                bundle.putString("volId", volId + "");
                bundle.putString("instId", instId);
                RouterManager.router(ARouterPathConstant.PRACTICE_TEAM_DETAIL,bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @OnClick(R2.id.back_btn)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setList(List<PracticeTeamBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        page++;
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(this)) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_RANK_TEAM);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_RANK_TEAM);
    }

}
